/**
 * 
 * 工具类
 * 
 */

Ext.namespace('ESBuilder.prm.util');
// 统一维护变量


/**
 * 
 * 打印指定内容。
 * 
 * @param {}
 *            values
 * 
 */
ESBuilder.prm.util.print = function(content) {
	var frame = Ext.get('print_iframe').dom;	
	if (Ext.isIE6 || Ext.isIE7 || Ext.isIE8) {
		var obj = null;
		if (frame.contentDocument) {
			obj = frame.contentDocument.getElementById('print_container');
		} else {
			obj = frame.document.getElementById('print_container');
		}
		obj.innerHTML = content;
		if (frame.contentWindow) {
			frame.contentWindow.focus();
			frame.contentWindow.print();
		} else {
			frame.focus();
			frame.print();
		}
	} else {
		var obj = frame.contentWindow.document
				.getElementById('print_container');
		obj.innerHTML = content;
		frame.contentWindow.focus();
		frame.contentWindow.print();
	}
}

ESBuilder.prm.util.columnRender = function(ds,param) {
	return function(value, metadata, record, rowIndex, colIndex, store) {
		var obj = ds.getById(value);
		if (obj == undefined) {
			return '未定义';
		}
		return param==undefined?obj.data.name:obj.data[param];
	};
}

ESBuilder.prm.util.Format = function() {
	return {
		/**
		 * Format a number as Chinese currency
		 * 
		 * @param {Number/String}
		 *            value The numeric value to format
		 * @return {String} The formatted currency string
		 */
		cnMoney : function(v) {
			if (isNaN(v)) {
				return "￥0.00";
			}
			v = (Math.round((v - 0) * 100)) / 100;
			v = (v == Math.floor(v)) ? v + ".00" : ((v * 10 == Math.floor(v
					* 10)) ? v + "0" : v);
			v = String(v);
			var ps = v.split('.');
			var whole = ps[0];
			var sub = ps[1] ? '.' + ps[1] : '.00';
			var r = /(\d+)(\d{3})/;
			while (r.test(whole)) {
				whole = whole.replace(r, '$1' + ',' + '$2');
			}
			v = whole + sub;
			if (v.charAt(0) == '-') {
				return '-￥' + v.substr(1);
			}
			return "￥" + v;
		}
	};
}();

Ext.namespace("ECard.ams.util");
// 验证日期组件的最小日期和最大日期
ECard.ams.util.dataValidate = {
	daterange : function(val, field) {
		var date = field.parseDate(val);

		if (!date) {
			return false;
		}
		if (field.startDateField
				&& (!this.dateRangeMax || (date.getTime() != this.dateRangeMax
						.getTime()))) {
			var start = Ext.getCmp(field.startDateField);
			start.setMaxValue(date);
			start.validate();
			this.dateRangeMax = date;
		} else if (field.endDateField
				&& (!this.dateRangeMin || (date.getTime() != this.dateRangeMin
						.getTime()))) {
			var end = Ext.getCmp(field.endDateField);
			end.setMinValue(date);
			end.validate();
			this.dateRangeMin = date;
		}
		return true;
	}
}

// 验证日期的最大，最小值
Ext.apply(Ext.form.VTypes, ECard.ams.util.dataValidate);



// 同步请求
ESBuilder.prm.util.form = function(Action, JSON) {
	var form = Ext.getDom('formParam');
	form.action = Action
	Ext.getDom('JSON').value = JSON;
	form.submit();
};